package com.ice.base.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.ice.base.dao.BaseDao;
import com.ice.base.dao.SysLoginDao;
import com.ice.base.model.SysLoginModel;
import com.ice.base.model.SysLoginRoleModel;
import com.ice.base.model.SysRoleModel;
import com.ice.base.service.SysLoginRoleService;
import com.ice.base.service.SysLoginService;
import com.ice.base.service.SysRoleService;
import com.ice.base.util.BaseUtil;
import com.ice.base.util.FaceUtil;
import com.ice.body.hardware.model.DeviceFace;
import com.ice.body.hardware.service.DeviceFaceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * Description:
 * Date: 2018-12-17
 * Time: 11:36
 */
@Slf4j
@Service
public class SysLoginServiceImpl extends BaseServiceImpl<SysLoginModel> implements SysLoginService {
    @Resource
    private SysLoginDao dao;
    @Resource
    private SysLoginRoleService sysLoginRoleService;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private DeviceFaceService deviceFaceService;
    @Resource
    private FaceUtil faceUtil;


    @Override
    public BaseDao<SysLoginModel> getBaseDao() {
        return dao;
    }





    @Override
    @Transactional
    public Integer insert(SysLoginModel model){
        SysLoginModel toFace = new SysLoginModel();
        model.setDelFlag(0);
        model.setIsFace(0);
        model.setCardNo(DateUtil.format(new Date(),"yyyyMMddHHmmss"));
        model.setUserNo(BaseUtil.generateRandomStr(6));// 使用5位随机数，防止重复
        BeanUtils.copyProperties(model,toFace);
        BaseUtil.encryptionPassword(model);
        if (dao.insert(model) != 0) {
            String warehouseFlag = model.getWarehouseFlag();
            if(StringUtils.isNotEmpty(warehouseFlag)){
                HashMap<String,Object> paramMap = new HashMap<>();
                //楼层标志
                paramMap.put("warehouseFlag",warehouseFlag);
                List<DeviceFace> list = deviceFaceService.getByFaceFlag(paramMap);
                //下发到设备
                if(CollectionUtil.isNotEmpty(list)){
                    for (DeviceFace a:list) {
                        faceUtil.addUserForFace(toFace,a.getIp(),a.getPort(),a.getUserName(),a.getPwd());
                    }
                }

            }
            //角色id不为空 给用户授权角色
               /* if (StringUtils.isNotEmpty(model.getRole_id())) {
                    SysLoginRoleModel sysLoginRoleModel = new SysLoginRoleModel();
                    sysLoginRoleModel.init(sysLoginRoleModel);
                    sysLoginRoleModel.setLogin_id(model.getId());
                    sysLoginRoleModel.setRole_id(model.getRole_id());
                    sysLoginRoleService.insert(sysLoginRoleModel);
                }*/
        }
        return 1;
    }


    @Transactional
    @Override
    public Integer insertSysLogin(SysLoginModel model) {
        //当前登录用户
        SysLoginModel currLoginModel = BaseUtil.getCurrLoginModel();
        //账号类型 0.admin 1.渠道 2.终端管理 3.终端用户
        /*        Integer handRole = currLoginModel.getHandRole();*/
        model.setCreateBy(currLoginModel.getUserName());//登录账号

        try{


            model.setUserNo(BaseUtil.generateRandomStr(6));// 使用5位随机数，防止重复
            model.setCardNo(BaseUtil.generateRandomStr(6));// 使用5位随机数，防止重复

            String pwd = model.getPassWord();
            BaseUtil.encryptionPassword(model);
            model.setWarehouseFlag("1");
            if (dao.insert(model) == 1){
                SysLoginModel sysLoginModel = this.selectModelByUserName(model.getUserName());
                SysLoginRoleModel sysLoginRoleModel = new SysLoginRoleModel();
                sysLoginRoleModel.init(sysLoginRoleModel);
                sysLoginRoleModel.setLogin_id(sysLoginModel.getId());
                sysLoginRoleModel.setRole_id(model.getRoleId());
                sysLoginRoleService.insert(sysLoginRoleModel);
                model.setPassWord(pwd);
                //下发到设备
                String warehouseFlag = model.getWarehouseFlag();
              //  if(StringUtils.isNotEmpty(warehouseFlag)) {
//                    HashMap<String, Object> paramMap = new HashMap<>();
//                    //楼层标志
//                    paramMap.put("warehouseFlag", warehouseFlag);
                    //查询出楼层 warehouseId标记的面部识别
                   // List<DeviceFaceModel> list = deviceFaceService.getByFaceFlag(paramMap);
                    ArrayList<DeviceFace> list = deviceFaceService.selectAll();
                    SysLoginModel toFace = new SysLoginModel();
                    //下发到设备
                    if (CollectionUtil.isNotEmpty(list)) {
                        for (DeviceFace a : list) {
                            faceUtil.addUserForFace(model, a.getIp(), a.getPort(), a.getUserName(), a.getPwd());
                        }
                    }
                //}
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public SysLoginModel getById(Integer id) {
        return dao.getById(id);
    }

    @Override
    public SysLoginModel selectModelByUserName(String SysLoginModel_name) {
        return dao.selectModelByUserName(SysLoginModel_name);
    }

    @Override
    public SysLoginModel selectDetailsById(Integer id) {
        SysLoginModel sysLoginModel = dao.getById(id);
        if (sysLoginModel != null) {
            return getLoginDetails(sysLoginModel);
        }
        return null;
    }

    /**
     * 查询用户权限明细
     * @param sysLoginModel
     * @return
     */
    @Override
    public SysLoginModel getLoginDetails(SysLoginModel sysLoginModel) {

        Integer login_id = sysLoginModel.getId();
        //查询用户角色关联表
        List<SysLoginRoleModel> sysLoginRoleModels = sysLoginRoleService.selectByLoginId(login_id);
        if (sysLoginRoleModels != null && sysLoginRoleModels.size() > 0) {
            ArrayList<SysRoleModel> sysRoleModels = new ArrayList<>();
            for (SysLoginRoleModel model : sysLoginRoleModels) {
                //查询角色
                SysRoleModel sysRoleModel = sysRoleService.selectDetailsById(model.getRole_id());
                sysRoleModels.add(sysRoleModel);
            }
            sysLoginModel.setRoles(sysRoleModels);
        }
        return sysLoginModel;
    }


    /**
     * 根据用户编号查询用户信息
     *
     * @param userNo
     * @return
     */
    @Override
    public SysLoginModel getByUserNo(String userNo){
        return dao.getByUserNo(userNo);
    }



    /**
     * 更新用户人脸识别状态
     * @param user
     * @return
     */
    @Override
    @Transactional
    public boolean modifyIsFaceByCardNo(SysLoginModel user){
        boolean flagVal = false;
        if(dao.update(user) > 0){
            flagVal = true;
        }
        return flagVal;
    }

    /**
     * 根据用户卡号查询用户人脸识别状态
     * @param cardNo
     * @return
     */
    @Override
    public int selectIsFaceByCardNo(String cardNo){
        int returnVal = 1;// 默认已经进行了人脸识别
        Integer isFaceVal = dao.selectIsFaceByCardNo(cardNo);
        if(isFaceVal != null){
            returnVal = isFaceVal.intValue();
        }
        return returnVal;
    }
}
